using IQIGame.Onigao.Framework;
using Lean.Touch;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 主角相关，交互操作等
    /// </summary>
    public class LevelPlayCharacterCtrl : LevelPlaySubBaseCtrl
    {
        /// <summary>
        /// 当前交互状态
        /// </summary>
        public FSMSystem<LevelInteractState, LevelInteractEvent> fsm { protected set; get; } = new FSMSystem<LevelInteractState, LevelInteractEvent>();
        public InteractStateOperational stateOperational => _stateOperational;

        private InteractStateOperational _stateOperational;
        private InteractStateInteractWithNPC _stateAttack;
        private InteractStateJump _stateJump;
        private InteractStateStandby _stateStandby;

        public LevelPlayCharacterCtrl()
        {
            AddMsgListener();
        }

        protected override void OnModuleGameplayInit()
        {

        }

        protected override void OnSceneLoaded()
        {
            //添加状态
            _stateOperational = new InteractStateOperational(this.regionCtrl);
            fsm.AddState(_stateOperational);
            _stateAttack = new InteractStateInteractWithNPC(this.regionCtrl);
            fsm.AddState(_stateAttack);
            _stateJump = new InteractStateJump(this.regionCtrl);
            fsm.AddState(_stateJump);
            _stateStandby = new InteractStateStandby(this.regionCtrl);
            fsm.AddState(_stateStandby);
        }

        public void Active()
        {
            //开始状态机
            fsm.Start(LevelInteractState.Operational);
        }

        //以下事件在_stateOperational销毁时会自动移除
        /// <summary>
        /// 添加手势侦听
        /// </summary>
        /// <param name="action"></param>
        public void AddTouchGestureEvent(Action<List<LeanFinger>> action)
        {
            this._stateOperational.onTouchGesture += action;
        }

        public void AddTouchTapEvent(Action<LeanFinger> action)
        {
            this._stateOperational.onTouchTap += action;
        }

        public void AddTouchUpEvent(Action<LeanFinger> action)
        {
            this._stateOperational.onTouchUp += action;
        }

        protected override void OnUpdate(float deltaTime)
        {
            base.OnUpdate(deltaTime);

            fsm.UpdateFSM(deltaTime);
        }

        private void AddMsgListener()
        {
            MsgDispatcher.AddListener<int>(MsgEventType.Level_NotifyChangePlayerCamera, NotifyPlayerCameraChange);
        }

        private void RemoveMsgListener()
        {
            MsgDispatcher.RemoveListener<int>(MsgEventType.Level_NotifyChangePlayerCamera, NotifyPlayerCameraChange);
        }

        public void NotifyPlayerCameraChange(int cameraCid)
        {
            levelPlayCtrl.levelPlayer.customCameraCid = cameraCid;
            if (cameraCid != 0)
            {
                regionCtrl.cameraCtrl.ChangeCharacterCamera(cameraCid);
            }
            else
            {
                regionCtrl.cameraCtrl.ResetCharacterCamera();
            }
        }

        /// <summary>
        /// 待命（暂不可操作）
        /// </summary>
        public void Standby()
        {
            fsm.ChangeState(LevelInteractState.Standby);
        }

        public void QuitStandby()
        {
            fsm.BroadcastEvent(LevelInteractEvent.QuitStandbyState);
        }

        public void EndFsm()
        {
            fsm?.Dispose();
            fsm = null;
        }

        protected override void OnDispose()
        {
            RemoveMsgListener();
            if (fsm != null)
            {
                EndFsm();
            }
        }
    }
}
